package com.winning.pregnancyandroid.activities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.example.pregnancyandroid.R;
import com.winning.pregnancyandroid.utils.HTTPGetTool;
import com.winning.pregnancyandroid.utils.URLUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class LoginActivity extends Activity {
	private EditText username;
	private EditText password;
	private Button loginbt;
	private TextView errinfo,regsiter;
	private ProgressDialog prodialog;
	private ImageView back;
	//private ActionBar actionbar = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.login);
		//actionbar=getActionBar();
		//actionbar.setTitle("");
		//actionbar.show();
		findViews(); //初始化样式
		setListener();

		
	}
	
	public  void findViews(){
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		errinfo = (TextView) findViewById(R.id.errinfo);
		loginbt = (Button) findViewById(R.id.loginbt);
		back = (ImageView) findViewById(R.id.back);
		regsiter = (TextView) findViewById(R.id.regsiter);
		
	}
	public void login(){
		String uname = username.getText().toString().trim();
		//String passwd = password.getText().toString().trim();
		if (uname.equals("")){
			errinfo.setText("用户名不能为空");
		}else{
			prodialog = new ProgressDialog(LoginActivity.this);
			prodialog.setMessage("登陆中...");
			prodialog.show();
			new LoginThread().start();
			
			

		}

	}
	Handler loginHandler = new Handler(){
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			prodialog.dismiss();
			switch(msg.what){
			case 1:
				Intent it = new Intent(LoginActivity.this, MainActivity.class);
				startActivity(it);
				break;
			case 2:
				Bundle data = msg.getData();
				errinfo.setText(data.getString("errinfo"));
				break;
			}
		};
	};
	public class LoginThread extends Thread{
		public void run() {
			String dlzh = username.getText().toString().trim();
			String dlmm = password.getText().toString().trim();			
			Map<String,String> map = new HashMap<String,String>();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			map.put("dlzh", dlzh);
			map.put("dlmm", dlmm);
			params.add(new BasicNameValuePair("jsonReq",JSON.toJSONString(map)));
			JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOSTMAIN+URLUtils.URLLOGIN, params);		
			try {
				Message msg = new Message();
				if(json.getBoolean("success")){
					msg.what = 1;
				}else{
					Bundle data = new Bundle();
					data.putString("errinfo", json.getString("err"));
					msg.setData(data);
					msg.what = 2;
				}
				loginHandler.sendMessage(msg);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		};


	}
	public void initData(){
		
	}
	
	
	private void setListener(){
		loginbt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				login();
			}
		});
		back.setOnClickListener(new OnClickListener() {
			
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		regsiter.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent it = new Intent(LoginActivity.this, Regsiter.class);
				startActivity(it);
			}
		});
	}
}
